package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.constant.OrderStatusConstants;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.view.refundapplication.RefundApplicationView;
import com.superman.supermarket.client.view.refundapplication.RefundApplicationView.RefundApplicationViewPresenter;

/**
 * 退货退款申请
 * 
 * @author superman
 * @version 2018年8月15日下午5:07:35
 */
public class RefundApplicationActivity extends ShopAbstractActivity implements RefundApplicationViewPresenter {

	private String orderid;
	@Inject
	RefundApplicationView refundApplicationView;
	private String skuid;

	private void gerRefundReasons() {
		postman.post("refund/reasons", null, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				Map<String, Object> map = XJsonUtil.getBody(response);

				List<String> reasonList = XJsonUtil.getStrList((JSONArray) map.get("REASONS"));
				refundApplicationView.setReasonList(reasonList);
			}
		});
	}

	private void getOrderDetail() {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ORDER_ID", orderid);
		postman.post("order/query2", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				parseOrderDetailMap(XJsonUtil.getBody(response));
			}
		});

	}

	@Override
	public IPageView getView() {
		return refundApplicationView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		return this;
	}

	@Override
	public void onActivity(EventBus eventBus) {
		// TODO Auto-generated method stub

	}

	private void parseOrderDetailMap(Map<String, Object> map) {
		int orderState = (int) Double.parseDouble(String.valueOf(map.get("ORDER_STATE")));
		boolean onlyRefundMoney = false;
		if (orderState == OrderStatusConstants.TO_DELIVERY.getStatus()) {
			// 待发货的状态,此时标题栏显示退款申请. 页面中的类型仅为退款,且退款的金额取退款sku的可退最大金额,不可编辑
			refundApplicationView.setPageHeaderText("退款申请");
			onlyRefundMoney = true;
		} else {
			// 其他状态,此时标题兰显示退款/退货申请,页面中的类型为退款和退货退款,且退款的金额取退款sku的可退最大金额,可编辑
			refundApplicationView.setPageHeaderText("退货/退款申请");
			onlyRefundMoney = false;
		}

		List<Map<String, Object>> storeList = XJsonUtil.getList((JSONArray) map.get("STORE_ORDER"));
		for (Map<String, Object> storeMap : storeList) {
			List<Map<String, Object>> skuList = XJsonUtil.getList((JSONArray) storeMap.get("SKU_LIST"));
			for (Map<String, Object> skuMap : skuList) {
				String thisSkuId = String.valueOf(skuMap.get("SKU_ID"));
				if (this.skuid.equals(thisSkuId)) {
					refundApplicationView.setRefundSkuInfo(skuMap, onlyRefundMoney);
					return;
				}
			}
		}
	}

	@Override
	public void popToRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void popToRefresh(Object contextData) {
		// TODO Auto-generated method stub

	}

	@Override
	public void pushToRefresh() {
		orderid = Window.Location.getParameter("orderid");
		skuid = Window.Location.getParameter("skuid");

		getOrderDetail();
		gerRefundReasons();
	}

	@Override
	public void submitApplication(Map<String, Object> map) {
		Map<String, Object> postMap = new HashMap<String, Object>();
		postMap.put("ORDER_ID", orderid);
		postMap.put("SKU_ID", skuid);
		postMap.putAll(map);
		postman.post("refund/applicate", postMap, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				eventBus.fireEvent(new ToastEvent("申请成功"));
				Map<String, Object> map = XJsonUtil.getBody(response);
				placeController.back(map);
			}
		});
	}

}
